﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using Infrastructure;
using MSIACCSL2.Web.WarehouseServices;

namespace MSIACCSL2.Web.ReportPage
{
    public partial class BTB : System.Web.UI.Page
    {
        private DataAccess<Object> DA;
        private ReportDocument _btb;
        private string Tipe;

        public BTB()
        {
            DA = new DataAccess<object>();
            _btb = new ReportDocument();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                LoadReport(Request.QueryString["no"].ToString(), RadioButtonList1.SelectedValue);
            }
        }


        public void LoadReport(string NoBTB, string tipe)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("Select a.NoBtb,a.TglBtb,a.[Description],a.PONumber,b.NoLog,b.NoBundle,b.QtyPack, ");
            sb.Append("b.Panjang,b.Lebar,b.Tinggi,b.QtyUnit,b.KodeMasuk,b.KodePallet,b.SatuanPack,b.SatuanUnit, ");
            sb.Append("c.SupplierName,i.ItemName,s.SubGroupName,d.KodeWarehouse ");
            sb.Append("from BTB a  ");
            sb.Append("inner Join BTBDetail b on a.NoBtb = b.NoBtb   ");
            sb.Append("Left Join Supplier c on c.SupplierId = a.SupplierId   ");
            sb.Append("Left Join Item i on i.ItemId = b.ItemId   ");
            sb.Append("Left Join Subgroup s on s.SubgroupId = i.SubgroupId ");
            sb.Append("Left Join Warehouse d on d.KodeWarehouse = b.KodeWarehouse ");
            sb.AppendFormat("Where a.NoBTB='{0}'  Order By s.SubgroupId,i.ItemId  ", NoBTB);



            var Source = DA.GetDataListDataSet(sb.ToString());
            string reportPath = Server.MapPath("~/Report/BTBDetail.rpt");
            _btb.Load(reportPath);
            _btb.SetDataSource(Source.Tables[0]);
            var param = new ParameterDiscreteValue();
            param.Value = tipe;
            var paramValue = new ParameterValues();
            paramValue.Add(param);
            _btb.DataDefinition.ParameterFields["group"].ApplyCurrentValues(paramValue);
         
            CrystalReportViewer1.ReportSource = _btb;

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            LoadReport(Request.QueryString["no"].ToString(), ViewState["tipe"].ToString());
        }

        protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            ViewState["tipe"] = RadioButtonList1.SelectedValue;
        }

        protected override void OnUnload(EventArgs e)
        {
            base.OnUnload(e);
            this.Unload += Page_Unload;
        }

        void Page_Unload(object sender, EventArgs e)
        {
            _btb.Clone();
            _btb.Dispose();
        }

      
    }
}